package com.zx.base.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.entity.UpdateInfo;

public class Update {

	private UpdateInfo up_info = new UpdateInfo();
	private TradeApp app;
	private UpdateManager mUpdateManager;
	private Context context;
	private double current_version = 1.0;
	private double new_version = 1.0;

	private ProgressDialog progressDialog;

	private boolean isDialog = false;

	public Update(Context context, TradeApp app, boolean isDialog) {
		this.context = context;
		this.app = app;
		this.isDialog = isDialog;
	}

	public void checkUpdate() {

		if (isDialog) {
			progressDialog = ProgressDialog.show(context, null, "正在获取升级信息，请稍后.....", true, true);
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "getNewVersion");
		if (!(app.user.getId().equals(""))) {
			map.put("userId", app.user.getId());
		}
		map.put("imei", app.getImei());
		map.put("imsi", app.getImsi());
		map.put("phone", app.getPhone());
		map.put("version", app.getVersion());
		map.put("phonetype", app.getPhoneType());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
				if (isDialog) {
					progressDialog.dismiss();
				}
			}

			@Override
			public void onFailure(Throwable e) {
				if (isDialog) {
					progressDialog.dismiss();
				}
			}

		});
	}

	// 解析获取的服务器json数据
	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				String data = response.getString("userData");
				String apkurl = new JSONObject(data).getString("apkurl");
				up_info.setApkurl(apkurl == null ? "" : apkurl);
				String apkname = new JSONObject(data).getString("apkname");
				up_info.setApkname(apkname == null ? "" : apkname);
				String apkversion = new JSONObject(data).getString("apkversion");
				up_info.setApkversion(apkversion == null ? "" : apkversion);
				String apkremark = new JSONObject(data).getString("apkremark");
				up_info.setApkremark(apkremark == null ? "" : apkremark);
				String apksize = new JSONObject(data).getString("apksize");
				up_info.setApksize(apksize == null ? "" : apksize);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			checkupdate();
		}
	}

	private void checkupdate() {

		if (!(up_info.getApkurl()).equals("")) {
			String url = URLHelper.mPrefix_main + up_info.getApkurl(); // http://192.168.1.210:8088/9qmy

			try {
				current_version = Double.parseDouble(getVersionName());
				new_version = Double.parseDouble(up_info.getApkversion());
			} catch (Exception e) {
			}

			if ((new_version > current_version) && (!(up_info.getApkurl().equals("")))) {
				mUpdateManager = new UpdateManager(context, url, up_info.getApkremark());
				mUpdateManager.checkUpdateInfo();
			}
		}

	}

	private String getVersionName() {
		String version_name = "";
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			version_name = packInfo.versionName;
		} catch (Exception e) {

		}
		return version_name;

	}

}
